package br.com.salao.dao;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import br.com.salao.bean.Login;
import br.com.salao.util.JpaUtil;


public class LoginDaoImpl implements LoginDao{
	
	private final GenericDao<Login> dao;
	
	public LoginDaoImpl(EntityManager em){
		dao = new GenericDao<Login>(em, Login.class);
	}
	
	public Login inserir(Login login){
		return dao.salvar(login);
	}
	
	public Login buscar(int id){
		return dao.buscar(id);
	}
		
	
	public Login buscarLogin(String login){
		
		EntityManager em = JpaUtil.getEntityManager();
		Login loginBD = null;
		
		try {		
			Query query = em.createQuery("from Login l where l.login like :user");
			query.setParameter("user", login);			
			loginBD = (Login) query.getSingleResult();
			
		} catch (Exception e) {
			loginBD = null;
		} finally{
			if(em != null){
				em.close();
			}
		}
		
		return loginBD;
		
	}

}
